class Transporte < ActiveRecord::Base
  
  before_save :data_faturamento
 
  belongs_to :status
  belongs_to :tipo
  belongs_to :deposito
  belongs_to :transportadora
  belongs_to :uf

  validates_presence_of :ndt
  validates_presence_of :pares
  validates_presence_of :cxs
  validates_presence_of :m
  validates_presence_of :peso
  validates_presence_of :box
  validates_presence_of :uf_id
  validates_presence_of :tipo_id
  validates_presence_of :status_id  
  validates_presence_of :deposito_id  
  validates_presence_of :transportadora_id

  validates_uniqueness_of :ndt
  
  validates_numericality_of :ndt
  validates_numericality_of :pares
  validates_numericality_of :cxs
  validates_numericality_of :m
  validates_numericality_of :peso
  validates_numericality_of :deposito_id
  validates_numericality_of :box  
  
  
  AttributesNames = {
    :ndt => "NDT",
    :transp => "TRANSP",
    :uf => "UF",
    :pares => "PARES",
    :cxs => "CXS",
    :tipo => "TIPO",
    :m => "VOLUME",
    :peso => "PESO",
    :box => "BOX",
    :status => "STATUS"
  };

  
  def to_label
    self.id
  end
  
  def ndt_novo
    "#{self.ndt.to_i}"
  end
  
  def pares_novo
    "#{self.pares.to_i.para_dinheiro.to_s_m}"
  end
  
  def cxs_novo
    "#{self.cxs.to_i.para_dinheiro.to_s_m}"
  end
  
  def m_novo
    "#{self.m.to_i.para_dinheiro.to_s_m}"
  end
  
  def peso_novo
    "#{self.peso.to_i.para_dinheiro.to_s_m}"
  end
  
  def data_inicio
    return self.created_at.to_s_br
  end
  
  def etq
    self.etiqueta ? "OK" : " - "
  end
  
  def data_faturamento
    #if self.status.sigla.downcase_br =~ /^fat/ or self.status.sigla.downcase_br =~ /^libe/
    if self.status.id == 9 or self.status.id == 4
      self.ended_at = DateTime.now
    else
      self.ended_at = nil
    end
  end
end
